

#include "tStockage.h"


void tStockage()
{
	for ( ; ; )
	{
		MsgPalette * msgPalette;
		Lot * lotCourant;		
		Palette * newPalette;
		char * buffer;
		char type;
		
		msgPalette = (MsgPalette *) malloc(sizeof(MsgPalette));				
		msgQReceive(balPalette, (char *) msgPalette, sizeof(MsgPalette), WAIT_FOREVER);
		
				if (msgPalette->num == MSG_FIN)
				{
					MsgOrdreExpe * msgOrdreExpe;
					msgOrdreExpe = (MsgOrdreExpe *) malloc(sizeof(MsgOrdreExpe));
					msgOrdreExpe->numCommande = MSG_FIN;
					
					msgQSend(balOrdreExpe, (char *) msgOrdreExpe, sizeof(MsgOrdreExpe), WAIT_FOREVER, MSG_PRI_NORMAL);
					free(msgOrdreExpe);
					DBG("Fin de tStockage");
					return;
				}
		
		newPalette = malloc(sizeof(Palette));
		newPalette->num = msgPalette->num;
		newPalette->lot = msgPalette->lot;
		newPalette->paletteSuivante = NULL;
		newPalette->emplacement = rand();
		
		lotCourant = entrepot;
		
		while (lotCourant->num != newPalette->lot->num)
		{
			lotCourant = (Lot*) lotCourant->lotSuivant;
		}
		
		newPalette->paletteSuivante = (struct Palette*) lotCourant->premierePalette;
		lotCourant->premierePalette = (struct Palette*) newPalette;
		lotCourant->palettesDispos += 1;
		entrepot=lotCourant;
		
		/*
		buffer = (char *) malloc(10 * sizeof(char));
		type = TRAME_VX_ETAT_LOT;
		memcpy(buffer, &type, 1);
		memcpy(buffer + 1, &(lotCourant->num), 4);
		memcpy(buffer + 5, &(lotCourant->palettesDispos), 4);
		memcpy(buffer + 9, &(lotCourant->type), 1);				
		msgQSend(balMessageLog, buffer, 10, WAIT_FOREVER, MSG_PRI_NORMAL);
		free(buffer);
		*/
		
		buffer = (char *) malloc(10 * sizeof(char));
				type = TRAME_VX_ETAT_LOT;
				memcpy(buffer, &type, 1);
				memcpy(buffer + 1, &(lotCourant->num), 4);
				memcpy(buffer + 5, &(lotCourant->palettesDispos), 4);
				memcpy(buffer + 9, &(lotCourant->type), 1);				
				msgQSend(balJournal, buffer, 10, WAIT_FOREVER, MSG_PRI_NORMAL);
				free(buffer);
		
		DBG("Le lot %d contient desormais %d palettes", lotCourant->num, lotCourant->palettesDispos);
	}
}
